<?php

class VFS_ftp {

    
    public function __construct($args = "")
    {
        parent::__construct($args);
    }
    
    public function chdir($path, $args = '') {
        if (!ftp_chdir($conn_id, "somedir")) {
            return(self::ERROR_CHDIR);
        } else { 
            return('');
        }
    }
    
    public function login($args = "")
    {
        $this->connection = ftp_connect($this->server);
        if (!$this->connection) {
            return(self::ERROR_CONNECT);
        }
        $this->login = ftp_login($this->connection, $this->username, $this->password);
        if ($this->login) {
            return('');
        } else {
            return(self::ERROR_LOGIN);
        }
    }    
    
    /**
     * Get a file from the server
     *
     *@param $file string The name of the file to get on the ftp servr
     *@todo Make this function for non-gtk version
     */
    public function get_contents($file, $args = "")
    {
        if ($this->gtk) {
            $this->data = "";
            $ftp = new VFS_ftp_task();
            $this->tmp = tempnam("/tmp", "phpgeteditor_");
            $ret = ftp_nb_get($this->connection, $this->tmp, $file, FTP_BINARY);
            if ($ret == FTP_MOREDATA) {
                $this->task_id = Gtk::idle_add(array($this, 'get_contents_continue'));
            } else {
                if ($ret != FTP_FINISHED) {
                    $this->data = self::ERROR_READ;
                    unlink($this->tmp);
                } else {
                    $this->data = file_get_contents($this->tmp);
                    unlink($this->tmp);
                }    
            }    
        } 
    }
    
    /**
     * This function is to read more data from a get_contents in gtk mode
     */
    public function get_contents_continue()
    {
        $ret = ftp_nb_continue($this->connection);
        if ($ret == FTP_MOREDATA) {
            return("");
        } elseif ($ret == FTP_FINISHED) {
            $this->data = file_get_contents($this->tmp);
            unlink($this->tmp);
            Gtk::idle_remove($this->task_id);
        } else {
            $this->data = self::ERROR_READ;
            unlink($this->tmp);
             Gtk::idle_remove($this->task_id);
        }    
    }

    public function pwd($args = '') {
        return ftp_pwd($this->connection);
    }
}
  

/*
<?php
// set up basic connection
$conn_id = ftp_connect($ftp_server); 

// login with username and password
$login_result = ftp_login($conn_id, $ftp_user_name, $ftp_user_pass); 

// check connection
if ((!$conn_id) || (!$login_result)) { 
        echo "FTP connection has failed!";
        echo "Attempted to connect to $ftp_server for user $ftp_user_name"; 
        exit; 
    } else {
        echo "Connected to $ftp_server, for user $ftp_user_name";
    }

// upload the file
$upload = ftp_put($conn_id, $destination_file, $source_file, FTP_BINARY); 

// check upload status
if (!$upload) { 
        echo "FTP upload has failed!";
    } else {
        echo "Uploaded $source_file to $ftp_server as $destination_file";
    }

// close the FTP stream 
ftp_close($conn_id); 
?>
*/